﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AssetAllocationSystem.Models;
using NHibernate;
using NHibernate.Criterion;

namespace AssetAllocationSystem.Repositories
{
    public class EmployeeRepository : RepositoryBase<Employee>
    {
        private ISession session;

        public EmployeeRepository(ISession session) : base(session)
        {
            this.session = session;
        }
        //public Employee FindById(Guid employeeId)
        //{
        //    ISession session = base.CreateSession();

        //    var employee = (Employee)DetachedCriteria
        //        .For<Employee>()
        //        .Add(Restrictions.Eq("Id", employeeId))
        //        .GetExecutableCriteria(session)
        //        .UniqueResult();

        //    return employee;
        //}

        public IList<Employee> FindByLocationCode(string code)
        {
            var employees = DetachedCriteria
                .For<Employee>()
                .CreateAlias("Location","loc")
                .Add(Restrictions.Like("loc.Code", code))
                .GetExecutableCriteria(session)
                .List<Employee>();

            return employees;
        }

        public IList<Employee> FindByPrimaryLocation(string primaryLocation)
        {
            var employees = DetachedCriteria
                .For<Employee>()
                .Add(Restrictions.Like("PrimaryLocation", primaryLocation))
                .GetExecutableCriteria(session)
                .List<Employee>();

            return employees;
        }

        public IList<Employee> FindByExistingLocation(string existingLocation)
        {
            var employees = DetachedCriteria
                .For<Employee>()
                .Add(Restrictions.Eq("ExistingLocation", existingLocation))
                .GetExecutableCriteria(session)
                .List<Employee>();

            return employees;
        }
    }
}